#!/usr/bin/perl

use DBI;
use strict;

my $output_debug=0;
# get our our command line arguments
for (my $i=0;$i<=$#ARGV;$i++){
    if ($ARGV[$i] eq "-D"){
	# level up debug printing
	$output_debug++;
    }
}

# database details
my $dsn="dbi:Pg:dbname=atca;host=mentok;port=5432";
my $user="postgres";
my $password="";

# Setup the database connection
my $dbh=DBI->connect($dsn,$user,$password,{ RaiseError => 0, AutoCommit => 1});

# Get a list of all the files in the kaputar archive directory
my @bad_project_codes=("JPL","POINT","BSLN",".junk");
my @potential_rpfits_files;
my @potential_rpfits_directories=("/DATA/ARCHIVE_1/ATCA/archive/");
@potential_rpfits_files=&list_directories("*.C*",@potential_rpfits_directories);

# get a list of all files already summarised
my $gts_query="SELECT DISTINCT rpfits_file FROM observation";
if ($output_debug>0){
    print "executing query: $gts_query\n";
}
my $gts=$dbh->prepare($gts_query);
$gts->execute;
while (my @gts_response=$gts->fetchrow){
    # find this file from our list and remove it
    for (my $i=0;$i<=$#potential_rpfits_files;$i++){
	if ($gts_response[0] eq $potential_rpfits_files[$i]){
	    splice @potential_rpfits_files,$i,1;
	    last;
	}
    }
}
$gts->finish;

if ($output_debug>1){
    for (my $i=0;$i<=$#potential_rpfits_files;$i++){
	print "RPFITS $i: $potential_rpfits_files[$i]\n";
    }
}

# go through the RPFITS files we've found - we leave off the last one because
# it may still be being written to by the correlator
for (my $i=0;$i<$#potential_rpfits_files;$i++){
    if ($output_debug>0){
	print "= examining file $potential_rpfits_files[$i]\n";
    }
    # check that this RPFITS file is one that we want to summarise
    my $projectcode=$potential_rpfits_files[$i];
    $projectcode=~s/.*\.(.*)/$1/;
    my $badcode=0;
    for (my $j=0;$j<=$#bad_project_codes;$j++){
	if ($projectcode eq $bad_project_codes[$j]){
	    $badcode=1;
	    last;
	}
    }
    if ($badcode==1){
	next;
    }
    if ($output_debug>0){
	print "= project code is OK\n";
    }
    # check that this RPFITS file hasn't already been summarised
    my $already_done=0;
    my $ckr=$dbh->prepare("SELECT rpfits_file FROM observation WHERE rpfits_file='".$potential_rpfits_files[$i]."'");
    $ckr->execute;
    while (my ($rpfits_file)=$ckr->fetchrow){
	if ($rpfits_file eq $potential_rpfits_files[$i]){
	    $already_done=1;
	    last;
	}
    }
    $ckr->finish;
    if ($already_done==1){
	if ($output_debug>0){
	    print "= file has already been summarised\n";
	}
	next;
    }
    if ($output_debug>0){
	print "= file has not already been summarised\n";
    }
    # get the array used for this file
    my $array_command="/n/ste616/bin/which_array.pl $potential_rpfits_files[$i]";
    if ($output_debug>0){
	print "= executing command $array_command\n";
    }
    my @array_stations;
    my $array_name;
    open(ARRAY,"-|")||exec $array_command;
    while(<ARRAY>){
	chomp;
	my $line=$_;
	my @els=split(/\s+/,$line);
	if ($line=~/^antenna/){
	    # the station this antenna is at
	    $array_stations[$els[1]]=$els[5];
	} elsif ($line=~/^best/){
	    $array_name=$els[4];
	}
    }
    close(ARRAY);
    my $stationsstring="{";
    for (my $i=1;$i<=6;$i++){
	$stationsstring.="\"".$array_stations[$i]."\",";
    }
    $stationsstring=~s/\,$/\}/;
    # now make the summary
    my $summary_command="/n/ste616/bin/rpfits_summary_for_atcadb $potential_rpfits_files[$i]";
    if ($output_debug>0){
	print "= executing command $summary_command\n";
    }
    open(SUMMARY,"-|")||exec $summary_command;
    while(<SUMMARY>){
	chomp;
	my $line=$_;
	# check for FITS file name lines
	if ($line=~/^\>\>\>/){
	    next;
	}
	if ($output_debug>1){
	    print "- got line !$line!\n";
	}
	# check for lines with no data
	my $good_data=$line;
	$good_data=~s/.*\[(.*)\].*/$1/;
	my $slew_data=$line;
	$slew_data=~s/.*\<(.*)\>.*/$1/;
	my @good_els=split(/\s+/,$good_data);
	my @slew_els=split(/\s+/,$slew_data);
	my $allzeros=1;
	for (my $j=0;$j<=$#good_els;$j++){
	    if ($good_els[$j]>0){
		$allzeros=0;
		last;
	    }
	}
	if ($allzeros==1){
	    for (my $j=0;$j<=$#slew_els;$j++){
		if ($slew_els[$j]>0){
		    $allzeros=0;
		    last;
		}
	    }
	}
	if ($allzeros==1){
	    if ($output_debug>1){
		print "- no usable data in this scan\n";
	    }
	    next;
	}
	# split up the line, but this can be complicated
	$line=~/^(.{4})\s(.{12})\s(.{8})\s(.{16})\s(.{16})\s(.{4})\s(.{10})\s(.{11})\s(.*)\s(.{3})\s(\[.*\])\s+(\<.*\>)/x;
	my $headernum=$1;
	my $date=$2;
	my $time=$3;
	my $scantype=$4;
	my $source=$5;
	my $calcode=$6;
	my $ra=$7;
	my $dec=$8;
	my $freqinfo=$9;
	my $cycletime=$10;
	my $onsource=$11;
	my $slewing=$12;
	# remove all the extraneous spaces
	$headernum=~s/^0*//;
	$date=~s/\s//g;
	$time=~s/\s//g;
	$scantype=~s/\s//g;
	$source=~s/\s//g;
	$calcode=~s/\s//g;
	$ra=~s/\s//g;
	$dec=~s/\s//g;
	$cycletime=~s/\s//g;
	my $unified_time=$date." ".$time;
	if ($output_debug>0){
	    print "found source = $source ($ra, $dec) \@ $unified_time:\n";
	    print "  labelled '$scantype', cycletime = $cycletime, onsource = $onsource, slewing = $slewing\n";
	    print "  calcode '$calcode', frequencies: $freqinfo, header number: $headernum\n";
	}
	# now we split up the frequency info
	my @freqels=split(/\)/,$freqinfo);
	if ($output_debug>0){
	    print "  found ".($#freqels+1)." IFs\n";
	}
	my @central_freq;
	my @bandwidth;
	my @nchans;
	my $centralfrequencystring="{";
	my $nchansstring="{";
	my $bandwidthstring="{";
	for (my $j=0;$j<=$#freqels;$j++){
	    $freqels[$j]=~/(.*)\s\((.*)\s(.*)c/;
	    $central_freq[$j]=$1;
	    $bandwidth[$j]=$2;
	    $nchans[$j]=$3;
	    $central_freq[$j]=~s/\s//g;
	    if ($output_debug>0){
		print "  IF ".($j+1).": ".$central_freq[$j].", $bandwidth[$j]/$nchans[$j]\n";
	    }
	    $centralfrequencystring.=$central_freq[$j].",";
	    $nchansstring.=$nchans[$j].",";
	    $bandwidthstring.=$bandwidth[$j].",";
	}
	$centralfrequencystring=~s/\,$/\}/;
	$nchansstring=~s/\,$/\}/;
	$bandwidthstring=~s/\,$/\}/;
	# now make the data input query
	$onsource=~s/\[/\{/;
	$onsource=~s/\]/\}/;
	$onsource=~s/\s+/\,/g;
	$slewing=~s/\</\{/;
	$slewing=~s/\>/\}/;
	$slewing=~s/\s+/\,/g;
	my $inp_query="INSERT INTO observation (source,ra,dec,n_if,if_freq,if_nchans,if_bw,time,arrayname,".
	    "project_code,rpfits_file,cycle_time,offsource,onsource,scantype,calcode,stations,header_number,".
	    "num_processed,processed_ids)".
	    " VALUES('".$source."',".$ra.",".$dec.",".($#freqels+1).
	    ",'".$centralfrequencystring."','".$nchansstring."','".$bandwidthstring."','".$unified_time."','".
	    $array_name."','".$projectcode."','".$potential_rpfits_files[$i]."',".$cycletime.",'".
	    $slewing."','".$onsource."','".$scantype."','".$calcode."','".$stationsstring."',".$headernum.",".
	    "0,'{NULL}'".
#	# nproc procid   id   az   el    mm atten 11 12 21 22 31  32   41   42   51   52   61   62
#	    "0,'{NULL}',NULL,NULL,NULL,'{NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL}',".
#	# subref 1 2    3    4    5    6      corcfg temp  chi   evec jyperk pra  pdec humid  smon  wind
#	    "'{NULL,NULL,NULL,NULL,NULL,NULL}',NULL, NULL, NULL, NULL, NULL, NULL,NULL, NULL, NULL, NULL,".
#	#  winddir xtsys ytsys xyphase
#	    "NULL, NULL, NULL, NULL".
	    ")";
	if ($output_debug>0){
	    print "$inp_query\n";
	}
	my $inq=$dbh->prepare($inp_query);
	$inq->execute;
	$inq->finish;
    }
    close(SUMMARY);

}

$dbh->disconnect;

sub list_directories {
    my ($pattern,@directories)=@_;
    my @listing;

    for (my $i=0;$i<=$#directories;$i++){
	my $thisdir=$directories[$i];
	$thisdir=~s/\/$//g;
	open(LS,"-|") || exec "ls -rt $thisdir/$pattern";
	while(<LS>){
	    chomp;
	    my $result=$_;
	    $result=~s/^.*\/([^\/]*)$/$1/;
	    push @listing,$thisdir."/".$result;
	}
	close(LS);
    }

    return @listing;
}
